﻿using System;
using System.Collections.Generic;
using System.Text;
using com.dz.ht.common;
using System.Text.RegularExpressions;
using System.Web;

namespace com.dz.ht.components
{
    public class BookSite
    {
        //url http://www.sdxsw.org/sort.php?sortid=0&orderbyid=0&page=2

        private string m_sdxsw_book_list = "http://www.shouda8.com/Book/ShowBookList.aspx?id=$1&page={0}"; //"http://www.sdxsw.org/sort.php?sortid=0&orderbyid=0&page={0}";

        DataManger m_db_mgr = new DataManger();


        /// <summary>
        /// 触发发送消息的事件
        /// </summary>
        public event SendMsgEventHandler SendMsg;

        #region 消息回送
        /// <summary>
        /// 发送消息到客户端
        /// </summary>
        /// <param name="i_msg"></param>
        private void SendMsg2Client(string i_msg)
        {
            SendMsgEventArgs e = new SendMsgEventArgs();
            e.Msg = i_msg;

            if (SendMsg != null)
            {
                SendMsg(this, e);
            }

        }

        #endregion

        public void GetAiYunBookList()
        {
            string t_url = "http://www.aiyun.com/asort/0/{0}.html";
            string t_book_url = string.Empty;
            for (int index = 1; index < 340; index++)
            {
                t_book_url = string.Format(t_url, index);
                GetAiYunBook(t_book_url);
            }
        }

        public void GetAiYunBook(string i_url)
        {
            string t_reg = "<td[^<>]*?class=[\'\"]?odd[\'\"]?>\\s*?<a[^<>]*?hrefs*=s*[\'\"]*[^\"\']*[\'\"]*[^<>]*?>(.*?)</a></td>\\s*?<td[^<>]*?><a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>.*?</a>";
            try
            {

                string t_html = NetSiteCatchManager.ReadUrl(i_url, Encoding.GetEncoding("gbk"));
                if (!string.IsNullOrEmpty(t_html))
                {
            
                    MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_ma != null && t_ma.Count > 0)
                    {
                        foreach (Match t_mc in t_ma)
                        {

                            int t_ret = m_db_mgr.AddBookSearch(t_mc.Groups[1].Value.ToString().Trim(),  t_mc.Groups[2].Value.ToString().Trim(), 34);
                            if (t_ret > 0)
                            {
                                SendMsg2Client(string.Format("收集到书籍 {0} 章节地址", t_mc.Groups[1].Value.ToString()));
                            }


                        }
                    }
                }


            }
            catch (Exception ex)
            {
                LogHelper.Error("收集手打小说网书籍url失败" + ex.ToString());

            }
        }

        public void GetSDXSWBookList()
        {
            for (int index = 1; index < 209; index++)
            {
                GetSDXSWBook(index);
            }
        }

        private void GetSDXSWBook(int i_page_index)
        {
            //string t_reg="<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*\\d+\\.html)[\'\"]*[^<>]*?>\\[目录\\]</a>[\\s&nbsp;]*?<a.*?>(.*?)</a>";
            string t_reg = "<a[^<>]*?hrefs*=s*[\'\"]*([^\"\']*)[\'\"]*[^<>]*?>\\s*?目录\\s*?</a>\\s*?\\]</li>\\s*?<li[^<>]*?>\\s*?<a[^<>]*?hrefs*=s*[\'\"]*[^\"\']*\\d+\\.html[\'\"]*[^<>]*?>(.*?)</a>";
            string t_url = string.Format(m_sdxsw_book_list, i_page_index);

            Dictionary<string, string> t_dic = null;
            int t_ret=0;
            try
            {
               // string t_html=NetSiteCatchManager.ReadUrl(t_url,Encoding.GetEncoding("gbk"));
                string t_html = NetSiteCatchManager.ReadUrl(t_url, Encoding.GetEncoding("gb2312"));
                if (!string.IsNullOrEmpty(t_html))
                {
                    t_dic = new Dictionary<string, string>();
                    MatchCollection t_ma = Regex.Matches(t_html, t_reg, RegexOptions.IgnoreCase | RegexOptions.Compiled);

                    if (t_ma != null && t_ma.Count > 0)
                    {
                        foreach (Match t_mc in t_ma)
                        {

                            t_ret = m_db_mgr.AddBookSearch(t_mc.Groups[2].Value.ToString(),string.Format("http://www.shouda8.com{0}", t_mc.Groups[1].Value.ToString()), 33);
                            if (t_ret > 0)
                            {
                                SendMsg2Client(string.Format("收集到书籍 {0} 章节地址", t_mc.Groups[2].Value.ToString()));
                            }
                           
                           
                        }
                    }
                }

          
            }
            catch (Exception ex)
            {
                LogHelper.Error("收集手打小说网书籍url失败" + ex.ToString());
             
            }
        }
    }
}
